﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

using ConstructionBidOnline.DAL;
using ConstructionBidOnline.DAL.dsCompanyInformationTableAdapters;
using ConstructionBidOnline.Common;

namespace ConstructionBidOnline.Biz
{
    /// <summary>
    /// Company Information Object
    /// </summary>

    public class CompanyInformation : BOBase
    {


        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected CompanyInformation() { }

        #endregion

        #region Public Properties

        #region Instance Properties

        /// <summary>
        /// PKID of the CompanyInformation.
        /// </summary>
        public int CompanyID
        {
            get { return _companyID; }
            set
            {
                if (_companyID != value)
                {
                    if (OnPropertyChanging("CompanyID", _companyID, value))
                    {
                        int oldValue = _companyID;
                        _companyID = value;

                        OnPropertyChanged("CompanyID", oldValue, value);
                    }
                }
            }
        }  private int _companyID = -1;

        /// <summary>
        /// Companyname.
        /// </summary>
        public string Companyname
        {
            get { return _companyname; }
            set
            {
                if (_companyname != value)
                {
                    if (OnPropertyChanging("Companyname", _companyname, value))
                    {
                        string oldValue = _companyname;
                        _companyname = value;
                        OnPropertyChanged("Companyname", oldValue, value);
                    }
                }
            }
        }  private string _companyname = null;


        /// <summary>
        /// CompanyTagLine.
        /// </summary>
        public string CompanyTagLine
        {
            get { return _companytagline; }
            set
            {
                if (_companytagline != value)
                {
                    if (OnPropertyChanging("CompanyTagLine", _companytagline, value))
                    {
                        string oldValue = _companytagline;
                        _companytagline = value;
                        OnPropertyChanged("CompanyTagLine", oldValue, value);
                    }
                }
            }
        }  private string _companytagline = null;


        /// <summary>
        /// YearFounded.
        /// </summary>
        public string YearFounded
        {
            get { return _yearFounded; }
            set
            {
                if (_yearFounded != value)
                {
                    if (OnPropertyChanging("YearFounded", _yearFounded, value))
                    {
                        string oldValue = _yearFounded;
                        _yearFounded = value;
                        OnPropertyChanged("YearFounded", oldValue, value);
                    }
                }
            }
        }  private string _yearFounded = null;


        /// <summary>
        /// CompanyOverview.
        /// </summary>
        public string CompanyOverview
        {
            get { return _companyOverview; }
            set
            {
                if (_companyOverview != value)
                {
                    if (OnPropertyChanging("CompanyOverview", _companyOverview, value))
                    {
                        string oldValue = _companyOverview;
                        _companyOverview = value;
                        OnPropertyChanged("CompanyOverview", oldValue, value);
                    }
                }
            }
        }  private string _companyOverview = null;



        /// <summary>
        /// ServiceDescription.
        /// </summary>
        public string ServiceDescription
        {
            get { return _serviceDescription; }
            set
            {
                if (_serviceDescription != value)
                {
                    if (OnPropertyChanging("ServiceDescription", _serviceDescription, value))
                    {
                        string oldValue = _serviceDescription;
                        _serviceDescription = value;
                        OnPropertyChanged("ServiceDescription", oldValue, value);
                    }
                }
            }
        }  private string _serviceDescription = null;



        /// <summary>
        /// Keywords.
        /// </summary>
        public string Keywords
        {
            get { return _keywords; }
            set
            {
                if (_keywords != value)
                {
                    if (OnPropertyChanging("Keywords", _keywords, value))
                    {
                        string oldValue = _keywords;
                        _keywords = value;
                        OnPropertyChanged("Keywords", oldValue, value);
                    }
                }
            }
        }  private string _keywords = null;



        /// <summary>
        /// CompanyMembership.
        /// </summary>
        public string CompanyMembership
        {
            get { return _companymembership; }
            set
            {
                if (_companymembership != value)
                {
                    if (OnPropertyChanging("CompanyMembership", _companymembership, value))
                    {
                        string oldValue = _companymembership;
                        _companymembership = value;
                        OnPropertyChanged("CompanyMembership", oldValue, value);
                    }
                }
            }
        }  private string _companymembership = null;


        /// <summary>
        /// CurrentBillingCycle.
        /// </summary>
        public string CurrentBillingCycle
        {
            get { return _currentBillingCycle; }
            set
            {
                if (_currentBillingCycle != value)
                {
                    if (OnPropertyChanging("CurrentBillingCycle", _currentBillingCycle, value))
                    {
                        string oldValue = _currentBillingCycle;
                        _currentBillingCycle = value;
                        OnPropertyChanged("CurrentBillingCycle", oldValue, value);
                    }
                }
            }
        }  private string _currentBillingCycle = null;



        /// <summary>
        /// CompanyDescription.
        /// </summary>
        public string CompanyDescription
        {
            get { return _companydescription; }
            set
            {
                if (_companydescription != value)
                {
                    if (OnPropertyChanging("CompanyDescription", _companydescription, value))
                    {
                        string oldValue = _companydescription;
                        _companydescription = value;
                        OnPropertyChanged("CompanyDescription", oldValue, value);
                    }
                }
            }
        }  private string _companydescription = null;


        /// <summary>
        /// True iff membership fees is given.
        /// </summary>
        public bool IsMembershipFeesGiven
        {
            get { return _isMembershipFeesGiven; }
            set
            {
                if (_isMembershipFeesGiven != value)
                {
                    if (OnPropertyChanging("IsMembershipFeesGiven", _isMembershipFeesGiven, value))
                    {
                        bool oldValue = _isMembershipFeesGiven;
                        _isMembershipFeesGiven = value;
                        OnPropertyChanged("IsMembershipFeesGiven", oldValue, value);
                    }
                }
            }
        }  private bool _isMembershipFeesGiven = false;



        /// <summary>
        /// MembershipStatus.
        /// </summary>
        public string MembershipStatus
        {
            get { return _membershipStatus; }
            set
            {
                if (_membershipStatus != value)
                {
                    if (OnPropertyChanging("MembershipStatus", _membershipStatus, value))
                    {
                        string oldValue = _membershipStatus;
                        _membershipStatus = value;
                        OnPropertyChanged("MembershipStatus", oldValue, value);
                    }
                }
            }
        }  private string _membershipStatus = null;


        /// <summary>
        /// Date/time at which Membership will expire.
        /// </summary>
        public DateTime? MembershipExpiryDate
        {
            get { return _membershipExpiryDate; }
            set
            {
                if (_membershipExpiryDate != value)
                {
                    if (OnPropertyChanging("MembershipExpiryDate", _membershipExpiryDate, value))
                    {
                        DateTime? oldValue = _membershipExpiryDate;
                        _membershipExpiryDate = value;
                        OnPropertyChanged("MembershipExpiryDate", oldValue, value);
                    }
                }
            }
        }  private DateTime? _membershipExpiryDate = null;

        /// <summary>
        /// WebSiteURL.
        /// </summary>
        public string WebSiteURL
        {
            get { return _websiteURL; }
            set
            {
                if (_websiteURL != value)
                {
                    if (OnPropertyChanging("WebSiteURL", _websiteURL, value))
                    {
                        string oldValue = _websiteURL;
                        _websiteURL = value;
                        OnPropertyChanged("WebSiteURL", oldValue, value);
                    }
                }
            }
        }  private string _websiteURL = null;

        /// <summary>
        /// Points.
        /// </summary>
        public int Points
        {
            get { return _points; }
            set
            {
                if (_points != value)
                {
                    if (OnPropertyChanging("Points", _points, value))
                    {
                        int oldValue = _points;
                        _points = value;

                        OnPropertyChanged("Points", oldValue, value);
                    }
                }
            }
        }  private int _points = -1;

        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty CompanyInformation object.
        /// </summary>
        public static CompanyInformation CreateNew()
        {
            CompanyInformation obj = new CompanyInformation();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;
            obj.IsMembershipFeesGiven = false;
            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static CompanyInformation CreateNew(CompanyInformationCollection collection)
        {
            CompanyInformation obj = CreateNew();

            // Get next/unique ID for the new object.
            int minVal = 0;
            foreach (CompanyInformation companyinformation in collection)
            {
                if (companyinformation.CompanyID < minVal)
                    minVal = companyinformation.CompanyID;
            }
            obj.CompanyID = --minVal;
            collection.Add(obj);

            return (obj);
        }

        #endregion

        #region GetAllCompanyInformation

        /// <summary>
        /// Returns a collection of all CompanyInformation.
        /// </summary>
        public static CompanyInformationCollection GetAllCompanyInformation()
        {
            CompanyInformationTableAdapter adapter = new CompanyInformationTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsCompanyInformation.CompanyInformationDataTable tbl = adapter.GetData(null);

            CompanyInformationCollection list = new CompanyInformationCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsCompanyInformation.CompanyInformationRow row in tbl.Rows)
                {
                    CompanyInformation companyinformation = new CompanyInformation();
                    RelationalToObject(companyinformation, row);
                    list.Add(companyinformation);
                }
            }

            return (list);
        }

        #endregion

        #region GetCompanyInformationByID

        /// <summary>
        /// Returns an CompanyInformation object for the specified 'CompanyInformationID' or null if no CompanyInformation with the specified parameter(s) exists.
        /// </summary>
        public static CompanyInformation GetCompanyInformationByID(int Companyid)
        {
            CompanyInformationTableAdapter adapter = new CompanyInformationTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            dsCompanyInformation.CompanyInformationDataTable tbl = adapter.GetData(Companyid);

            if (tbl.Rows.Count == 1)
            {
                CompanyInformation companyinformation = new CompanyInformation();
                RelationalToObject(companyinformation, tbl.Rows[0]);
                return (companyinformation);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'CompanyInformation' returned more than one record.");

            return (null);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the specified CompanyInformation.
        /// </summary>
        public static void Update(CompanyInformation obj)
        {
            bool updateCompanyInformation = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updateCompanyInformation = (obj.ObjState != ObjectState.Unchanged);

                if (!updateCompanyInformation)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsCompanyInformation.CompanyInformationDataTable tbl = null;
            if (updateCompanyInformation)
            {
                CompanyInformationTableAdapter adapter = new CompanyInformationTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();

                tbl = new dsCompanyInformation.CompanyInformationDataTable();
                dsCompanyInformation.CompanyInformationRow row = tbl.NewCompanyInformationRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted) && (CompanyID <= 0))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("CompanyID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

                // UserName is required.
                if ((Companyname == null) || (Companyname.Trim().Length == 0))
                {
                    Errors.Add(new ValidationError("CompanyName", "CompanyName is required.", ValidationError.Severity.Error));
                }
                else if (ObjState == ObjectState.Added)
                {

                }

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region ToString

        /// <summary>
        /// User-friendly string represetation of this object.
        /// </summary>
        public override string ToString()
        {
            return (String.Format("CompanyInformation: CompanyID={0}, Companyname={1}, {2}", CompanyID, Companyname, CompanyTagLine));
        }

        #endregion

        #endregion

        #region Privates


        #endregion

    }

    /// <summary>
    /// Collection of 0 or more CompanyInformation objects.
    /// </summary>

    public class CompanyInformationCollection : List<CompanyInformation>
    {
        #region Types

        public class UserComparer : IComparer<CompanyInformation>
        {
            public int Compare(CompanyInformation ci1, CompanyInformation ci2)
            {
                return (String.Compare(ci1.Companyname, ci2.Companyname));
            }
        }

        #endregion

        #region Public Methods

        #region FindByCompanyID

        public CompanyInformation FindByCompanyID(int companyID)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].CompanyID == companyID)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #region FindCompanyName

        /// <summary>
        /// Case insensitve compare.
        /// </summary>
        public CompanyInformation FindCompanyName(string companyname)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].Companyname == companyname)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #endregion
    }
}
